package com.tbit.uqbike.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.object.value.WeatherResponse;
import com.tbit.uqbike.service.base.RedisService;
import com.tbit.uqbike.service.business.WeatherService;
import com.tbit.uqbike.webmanager.util.weathers.WeatherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author jing
 * @date 2020/11/1713:49
 * @Description:天气
 */
@Service("weatherService")
public class WeatherServiceImpl implements WeatherService {
    @Autowired
    private RedisService redisService;

    @Override
    public WeatherResponse.NowBean getNowWeather(Double lon, Double lat, Integer areaId) {
        String redisKey = areaId + ".weather_redis";
        if (redisService.hasKey(redisKey)) {
            return JSON.parseObject(redisService.get(redisKey), WeatherResponse.NowBean.class);
        } else {
            redisService.add(redisKey, JSON.toJSONString(WeatherUtil.getNowWeather(lon, lat)), 3600);
        }
        return WeatherUtil.getNowWeather(lon, lat);
    }
}
